//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Model.Core;

namespace Eas.WebCrawler.Model {

    /// <summary>This factory class constructs the <see cref="Eas.WebCrawler.Interfaces.WebCrawlerEngine"/> which represents the main class in the object model for crawling the web.</summary>
    public static class WebCrawlerFactory {

        /// <summary>Creates a <see cref="Eas.WebCrawler.Interfaces.WebCrawlerEngine"/> from a <see cref="Eas.WebCrawler.Interfaces.WebCrawlerSettings"/>.</summary>
        /// <param name="settings">A <see cref="Eas.WebCrawler.Interfaces.WebCrawlerSettings"/> that specifies how the created engine should act.</param>
        /// <returns>An implementation of <see cref="Eas.WebCrawler.Interfaces.WebCrawlerEngine"/>.</returns>
        public static Eas.WebCrawler.Interfaces.WebCrawlerEngine Create(Eas.WebCrawler.Interfaces.WebCrawlerSettings settings) {
            return Create(settings, new Eas.WebCrawler.Model.Gateway.BaseWebGateway(), new Eas.WebCrawler.Model.Actions.BaseActionFactory());
        }

        internal static Eas.WebCrawler.Interfaces.WebCrawlerEngine Create(Eas.WebCrawler.Interfaces.WebCrawlerSettings settings,  Eas.WebCrawler.Model.Gateway.WebGateway gateway, Eas.WebCrawler.Model.Actions.ActionFactory actionFactory) {
            WorkerContext workerContext = CreateWorkerContext(settings, gateway, actionFactory);
            BaseWebCrawlerEngine returnValue = new BaseWebCrawlerEngine(workerContext);
            return returnValue;
        }

        internal static WorkerContext CreateWorkerContext(Eas.WebCrawler.Interfaces.WebCrawlerSettings settings, Eas.WebCrawler.Model.Gateway.WebGateway gateway, Eas.WebCrawler.Model.Actions.ActionFactory actionFactory) {
            Eas.WebCrawler.Model.Events.BaseWebCrawlerEvents events = new Eas.WebCrawler.Model.Events.BaseWebCrawlerEvents();
            BaseRequestStore store = new BaseRequestStore();
            Eas.WebCrawler.Model.StateTrackers.BaseStateTracker stateTracker = new Eas.WebCrawler.Model.StateTrackers.BaseStateTracker();
            BaseWebCrawlerContext webCrawlerContext = new BaseWebCrawlerContext(settings, store, events);

            WorkerContext workerContext = new WorkerContext(webCrawlerContext, actionFactory, events, gateway, events, stateTracker);
            return workerContext;
        }
    }
}
